package com.wz;

import org.junit.jupiter.api.Test;

import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;

public class StringTest {
	@Test
	public void Test1() {
		String str = "      Thisis a String     ";
		String str1 = str.toLowerCase(); //str1="thisis a string";   实现大小写转换
		System.out.println(str1);
		String str2 = str.trim();   //去掉开头结尾空格
		System.out.println(str2);
		System.out.println("---------");
		String a = String.valueOf(Math.PI);
		System.out.println(String.valueOf(Math.PI));
		System.out.println("a==" + a);
		StringBuffer str3 = new StringBuffer("Thisis a String");
		str3.insert(9, "test");
		System.out.println(str3.toString());
	}

	@Test
	public void c() {
		String str = "Thisis a String";
		char[] chr = new char[12];
		str.getChars(1, 12, chr, 0);  //从光标 1之后开始截取 一直到第12（包含第十二）
		System.out.println(chr);
	}

	@Test
	public void d() {
		String str = "Thisis a String,java,php,pthyon,javaScript";
		//String[] strs = splitString(str, ",");
		//HashMap map = new HashMap();
		String[] strs = str.split(",");

		for (int i = 0; i < strs.length; i++) {
			System.out.println(strs[i]);
			//map.put(i, strs[i]);
		}
		/*System.out.println("通过Map.keySet遍历key和value：");
		for (Object key : map.keySet()) {
			System.out.println(key + "=>" + map.get(key));
		}*/
	}

	public String[] splitString(String str, String sdelimiter) {
		String[] array = str.split(sdelimiter);
		return array;
	}

	@Test
	public void e() {
		int strs[] = new int[]{4, 2, 1, 6, 7, 8, 9, 2, 4, 6, 0, 0, 0, 0};
		for (int i = 0; i < strs.length - 1; i++) {                // 外层循环，控制总共需要比较的趟数
			for (int j = 0; j < strs.length - i - 1; j++) {        // 内层循环，控制每趟需要比较的次数
				if (strs[j] > strs[j + 1]) {                    // 比较相邻元素// 以下三行代码用于交换两个元素
					int temp = strs[j];
					strs[j] = strs[j + 1];
					strs[j + 1] = temp;
				}
			}
		}
		for (int i = 0; i < strs.length; i++) {
			System.out.println(strs[i]);
		}
		//	System.out.println(strs[j]);
	}

	@Test
	public void b() {
		// 计算圆周率π:pi=4*(1-1/3+1/5-1/7+...)=3.1415926335902506
		double pi = 0;
		for (double i = 1; i <= 999999999; i += 2) { //为了方便计算，循环10亿次
			if (i % 4 == 3) {
				pi = pi - 1 / i;
			} else {
				pi = pi + 1 / i;
			}
		}
		System.out.println("pi=4*(1-1/3+1/5-1/7+...)=" + 4 * pi);

	}

	/**
	 * @param // []{}
	 * @String 找出A数组中存在而B数组中不存在的数
	 */
	public String[] compareArray(String[] A, String[] B) {
		Hashtable<String, String> hash = new Hashtable<String, String>();
		Hashtable<String, String> hash_new = new Hashtable<String, String>();

		for (int i = 0; i < B.length; i++)
			hash.put(B[i], B[i]);

		for (int i = 0; i < A.length; i++) {
			if (!hash.containsKey(A[i])) {
				hash_new.put(A[i], A[i]);
			}
		}
		String[] C = new String[hash_new.size()];
		int i = 0;
		Enumeration enumeration = hash_new.keys();
		while (enumeration.hasMoreElements()) {
			C[i] = enumeration.nextElement().toString();
			i++;
		}
		return C;
	}

	/**
	 * 合并两个数组去重
	 *
	 * @param A
	 * @param B
	 * @return
	 */
	public String[] compareArrays(String[] A, String[] B) {
		HashSet<String> set = new HashSet();
		for (int i = 0; i < A.length; i++) {
			set.add(A[i]);
		}
		for (int i = 0; i < B.length; i++) {
			set.add(B[i]);
		}
		String[] C = new String[set.size()];
		int i = 0;
		for (String o : set) {
			C[i] = o;
			i++;
		}
		return C;
	}

	/**
	 * 找到两个数组中相互没有的
	 *
	 * @param A
	 * @param B
	 * @return
	 */
	public String[] compareArrayss(String[] A, String[] B) {
		HashSet<String> set = new HashSet();
		HashSet<String> set1 = new HashSet();
		HashSet<String> set2 = new HashSet();
		for (int i = 0; i < A.length; i++) {
			set.add(A[i]);
		}
		for (int i = 0; i < B.length; i++) {
			if (!set.contains(B[i])) {
				set1.add(B[i]);
			}
		}
		for (int i = 0; i < B.length; i++) {
			set2.add(B[i]);
		}
		for (int i = 0; i < A.length; i++) {
			if (!set2.contains(A[i])) {
				set1.add(A[i]);
			}
		}
		String[] C = new String[set1.size()];
		int i = 0;
		for (String o : set1) {
			C[i] = o;
			i++;
		}
		return C;
	}

	@Test
	public void f() {
		String[] mobile1 = {"13811071500", "13811071501", "13811071502", "13811071503", "13811071504", "0", "12"};
		String[] mobile2 = {"13811071500", "13811071505", "13811071502", "13811071506", "13811071504", "123"};
		String[] mobile3 = compareArray(mobile1, mobile2);
		for (int i = 0; i < mobile3.length; i++)
			System.out.println(mobile3[i]);
		System.out.println("--------------------------");
		String[] mobile4 = compareArrays(mobile1, mobile2);
		for (int i = 0; i < mobile4.length; i++)
			System.out.println(mobile4[i]);
	}

	@Test
	public void g() {
		String[] mobile1 = {"13811071500", "13811071501", "13811071502", "13811071503", "13811071504", "0", "12"};
		String[] mobile2 = {"13811071500", "13811071505", "13811071502", "13811071506", "13811071504", "123"};
		String[] mobile4 = compareArrayss(mobile1, mobile2);
		for (int i = 0; i < mobile4.length; i++)
			System.out.println(mobile4[i]);
	}

	@Test
	public void h() {
		int a = 1;
		int b = 1;
		String aa = new String("1");
		String bb = new String("1");
		System.out.println(a == b);
		System.out.println(aa == bb);
		ThreadFactory f = new ThreadFactory() {
			private AtomicInteger atomicInteger = new AtomicInteger();

			@Override
			public Thread newThread(Runnable r) {
				atomicInteger.addAndGet(1);
				return new Thread(r, "自定义名字-" + atomicInteger.get());
			}
		};
		ThreadPoolExecutor ThreadPoolExecutor;
	}

}

